package by.koleda.photoupdater;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
import by.koleda.photoupdater.helpers.ImageLoader;
import by.koleda.photoupdater.model.Suggestion;

public class SuggestionsAdapter extends ArrayAdapter<Suggestion> {
	
	private final ArrayList<Suggestion> data;
	private LayoutInflater mInflater;
	private Context mContext;
	
	public SuggestionsAdapter(Activity context, ArrayList<Suggestion> data) {
		super(context, R.layout.adapter_suggestions, data);
		this.mContext = context;
		this.data = data;
		mInflater = context.getLayoutInflater();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final ViewHolder holder;
		
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.adapter_suggestions, null);
			
			holder = new ViewHolder();
		
			holder.contactName = (TextView) convertView.findViewById(R.id.suggestions_contact_name);
			holder.contactPhone = (TextView) convertView.findViewById(R.id.suggestions_contact_phone);
			holder.vkFirstName = (TextView) convertView.findViewById(R.id.suggestions_contact_firstname);
			holder.vkLastName = (TextView) convertView.findViewById(R.id.suggestions_contact_lastname);
			holder.contactPhoto = (ImageView) convertView.findViewById(R.id.suggestions_contact_image);
			holder.contactCheckBox = (CheckBox) convertView.findViewById(R.id.suggestions_contact_check);
			holder.contactCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					Suggestion suggestion = (Suggestion) holder.contactCheckBox.getTag();
					suggestion.setSelected(buttonView.isChecked());
				}
			});
			
			convertView.setTag(holder);
			holder.contactCheckBox.setTag(data.get(position));
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.contactCheckBox.setTag(data.get(position));
		}
		
		final Suggestion suggestion = data.get(position);
		
		holder.contactName.setText(suggestion.getDisplayName());
		holder.contactPhone.setText(suggestion.getPhoneNumber());
		holder.vkFirstName.setText(suggestion.getFirstName());
		holder.vkLastName.setText(suggestion.getLastName());
		holder.contactCheckBox.setChecked(suggestion.isSelected());
		ImageLoader.get(mContext).loadContactBitmap(holder.contactPhoto, suggestion.getContactId());
		
		return convertView;
	}
	
	static class ViewHolder {
        TextView contactName;
        TextView contactPhone;
        TextView vkFirstName;
        TextView vkLastName;
        ImageView contactPhoto;
        CheckBox contactCheckBox;
    }

}
